package com.evil.file.enums.log;

import com.evil.common.log.enums.base.LogType;
import com.evil.file.pojo.entity.*;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Getter
@AllArgsConstructor
public enum FileLogTypeEnum implements LogType {

    FILE(5000, "文件表", File.class),
    FILE_DOWNLOAD_HISTORY(5001, "文件下载历史表", FileDownloadHistory.class),
    ;

    private final int type;
    private final String name;
    private final Class<?> entityClass;

    public static Optional<FileLogTypeEnum> isExistByType(Integer type) {
        if (null == type) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type == type).findFirst();
    }

    public static Optional<FileLogTypeEnum> isExistByClass(Class<?> entityClass) {
        if (null == entityClass) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> null != e.entityClass && e.entityClass.equals(entityClass)).findFirst();
    }

    public static FileLogTypeEnum findByClass(Class<?> entityClass) {
        return FileLogTypeEnum.isExistByClass(entityClass).orElseThrow(() -> LogType.error(entityClass));
    }
}
